{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction to DataFrames\n",
    "**[Bogumił Kamiński](http://bogumilkaminski.pl/about/), Apr 21, 2018**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "using DataFrames # load package\n",
    "srand(1);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Manipulating rows of DataFrame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Reordering rows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>id</th><th>x</th><th>y</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>0.236033</td><td>0.0</td></tr><tr><th>2</th><td>2</td><td>0.346517</td><td>0.0</td></tr><tr><th>3</th><td>3</td><td>0.312707</td><td>0.0</td></tr><tr><th>4</th><td>4</td><td>0.00790928</td><td>0.0</td></tr><tr><th>5</th><td>5</td><td>0.488613</td><td>0.0</td></tr><tr><th>6</th><td>6</td><td>0.210968</td><td>1.0</td></tr><tr><th>7</th><td>7</td><td>0.951916</td><td>1.0</td></tr><tr><th>8</th><td>8</td><td>0.999905</td><td>1.0</td></tr><tr><th>9</th><td>9</td><td>0.251662</td><td>1.0</td></tr><tr><th>10</th><td>10</td><td>0.986666</td><td>1.0</td></tr></tbody></table>"
      ],
      "text/plain": [
       "10×3 DataFrames.DataFrame\n",
       "│ Row │ id │ x          │ y   │\n",
       "├─────┼────┼────────────┼─────┤\n",
       "│ 1   │ 1  │ 0.236033   │ 0.0 │\n",
       "│ 2   │ 2  │ 0.346517   │ 0.0 │\n",
       "│ 3   │ 3  │ 0.312707   │ 0.0 │\n",
       "│ 4   │ 4  │ 0.00790928 │ 0.0 │\n",
       "│ 5   │ 5  │ 0.488613   │ 0.0 │\n",
       "│ 6   │ 6  │ 0.210968   │ 1.0 │\n",
       "│ 7   │ 7  │ 0.951916   │ 1.0 │\n",
       "│ 8   │ 8  │ 0.999905   │ 1.0 │\n",
       "│ 9   │ 9  │ 0.251662   │ 1.0 │\n",
       "│ 10  │ 10 │ 0.986666   │ 1.0 │"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = DataFrame(id=1:10, x = rand(10), y = [zeros(5); ones(5)]) # and we hope that x[:x] is not sorted :)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(true, false)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "issorted(x), issorted(x, :x) # check if a DataFrame or a subset of its columns is sorted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>id</th><th>x</th><th>y</th></tr></thead><tbody><tr><th>1</th><td>4</td><td>0.00790928</td><td>0.0</td></tr><tr><th>2</th><td>6</td><td>0.210968</td><td>1.0</td></tr><tr><th>3</th><td>1</td><td>0.236033</td><td>0.0</td></tr><tr><th>4</th><td>9</td><td>0.251662</td><td>1.0</td></tr><tr><th>5</th><td>3</td><td>0.312707</td><td>0.0</td></tr><tr><th>6</th><td>2</td><td>0.346517</td><td>0.0</td></tr><tr><th>7</th><td>5</td><td>0.488613</td><td>0.0</td></tr><tr><th>8</th><td>7</td><td>0.951916</td><td>1.0</td></tr><tr><th>9</th><td>10</td><td>0.986666</td><td>1.0</td></tr><tr><th>10</th><td>8</td><td>0.999905</td><td>1.0</td></tr></tbody></table>"
      ],
      "text/plain": [
       "10×3 DataFrames.DataFrame\n",
       "│ Row │ id │ x          │ y   │\n",
       "├─────┼────┼────────────┼─────┤\n",
       "│ 1   │ 4  │ 0.00790928 │ 0.0 │\n",
       "│ 2   │ 6  │ 0.210968   │ 1.0 │\n",
       "│ 3   │ 1  │ 0.236033   │ 0.0 │\n",
       "│ 4   │ 9  │ 0.251662   │ 1.0 │\n",
       "│ 5   │ 3  │ 0.312707   │ 0.0 │\n",
       "│ 6   │ 2  │ 0.346517   │ 0.0 │\n",
       "│ 7   │ 5  │ 0.488613   │ 0.0 │\n",
       "│ 8   │ 7  │ 0.951916   │ 1.0 │\n",
       "│ 9   │ 10 │ 0.986666   │ 1.0 │\n",
       "│ 10  │ 8  │ 0.999905   │ 1.0 │"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sort!(x, :x) # sort x in place"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>id</th><th>x</th><th>y</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>0.236033</td><td>0.0</td></tr><tr><th>2</th><td>2</td><td>0.346517</td><td>0.0</td></tr><tr><th>3</th><td>3</td><td>0.312707</td><td>0.0</td></tr><tr><th>4</th><td>4</td><td>0.00790928</td><td>0.0</td></tr><tr><th>5</th><td>5</td><td>0.488613</td><td>0.0</td></tr><tr><th>6</th><td>6</td><td>0.210968</td><td>1.0</td></tr><tr><th>7</th><td>7</td><td>0.951916</td><td>1.0</td></tr><tr><th>8</th><td>8</td><td>0.999905</td><td>1.0</td></tr><tr><th>9</th><td>9</td><td>0.251662</td><td>1.0</td></tr><tr><th>10</th><td>10</td><td>0.986666</td><td>1.0</td></tr></tbody></table>"
      ],
      "text/plain": [
       "10×3 DataFrames.DataFrame\n",
       "│ Row │ id │ x          │ y   │\n",
       "├─────┼────┼────────────┼─────┤\n",
       "│ 1   │ 1  │ 0.236033   │ 0.0 │\n",
       "│ 2   │ 2  │ 0.346517   │ 0.0 │\n",
       "│ 3   │ 3  │ 0.312707   │ 0.0 │\n",
       "│ 4   │ 4  │ 0.00790928 │ 0.0 │\n",
       "│ 5   │ 5  │ 0.488613   │ 0.0 │\n",
       "│ 6   │ 6  │ 0.210968   │ 1.0 │\n",
       "│ 7   │ 7  │ 0.951916   │ 1.0 │\n",
       "│ 8   │ 8  │ 0.999905   │ 1.0 │\n",
       "│ 9   │ 9  │ 0.251662   │ 1.0 │\n",
       "│ 10  │ 10 │ 0.986666   │ 1.0 │"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = sort(x, :id) # new DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>id</th><th>x</th><th>y</th></tr></thead><tbody><tr><th>1</th><td>6</td><td>0.210968</td><td>1.0</td></tr><tr><th>2</th><td>9</td><td>0.251662</td><td>1.0</td></tr><tr><th>3</th><td>7</td><td>0.951916</td><td>1.0</td></tr><tr><th>4</th><td>10</td><td>0.986666</td><td>1.0</td></tr><tr><th>5</th><td>8</td><td>0.999905</td><td>1.0</td></tr><tr><th>6</th><td>4</td><td>0.00790928</td><td>0.0</td></tr><tr><th>7</th><td>1</td><td>0.236033</td><td>0.0</td></tr><tr><th>8</th><td>3</td><td>0.312707</td><td>0.0</td></tr><tr><th>9</th><td>2</td><td>0.346517</td><td>0.0</td></tr><tr><th>10</th><td>5</td><td>0.488613</td><td>0.0</td></tr></tbody></table>"
      ],
      "text/plain": [
       "10×3 DataFrames.DataFrame\n",
       "│ Row │ id │ x          │ y   │\n",
       "├─────┼────┼────────────┼─────┤\n",
       "│ 1   │ 6  │ 0.210968   │ 1.0 │\n",
       "│ 2   │ 9  │ 0.251662   │ 1.0 │\n",
       "│ 3   │ 7  │ 0.951916   │ 1.0 │\n",
       "│ 4   │ 10 │ 0.986666   │ 1.0 │\n",
       "│ 5   │ 8  │ 0.999905   │ 1.0 │\n",
       "│ 6   │ 4  │ 0.00790928 │ 0.0 │\n",
       "│ 7   │ 1  │ 0.236033   │ 0.0 │\n",
       "│ 8   │ 3  │ 0.312707   │ 0.0 │\n",
       "│ 9   │ 2  │ 0.346517   │ 0.0 │\n",
       "│ 10  │ 5  │ 0.488613   │ 0.0 │"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sort(x, (:y, :x), rev=(true, false)) # sort by two columns, first is decreasing, second is increasing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>id</th><th>x</th><th>y</th></tr></thead><tbody><tr><th>1</th><td>6</td><td>0.210968</td><td>1.0</td></tr><tr><th>2</th><td>9</td><td>0.251662</td><td>1.0</td></tr><tr><th>3</th><td>7</td><td>0.951916</td><td>1.0</td></tr><tr><th>4</th><td>10</td><td>0.986666</td><td>1.0</td></tr><tr><th>5</th><td>8</td><td>0.999905</td><td>1.0</td></tr><tr><th>6</th><td>4</td><td>0.00790928</td><td>0.0</td></tr><tr><th>7</th><td>1</td><td>0.236033</td><td>0.0</td></tr><tr><th>8</th><td>3</td><td>0.312707</td><td>0.0</td></tr><tr><th>9</th><td>2</td><td>0.346517</td><td>0.0</td></tr><tr><th>10</th><td>5</td><td>0.488613</td><td>0.0</td></tr></tbody></table>"
      ],
      "text/plain": [
       "10×3 DataFrames.DataFrame\n",
       "│ Row │ id │ x          │ y   │\n",
       "├─────┼────┼────────────┼─────┤\n",
       "│ 1   │ 6  │ 0.210968   │ 1.0 │\n",
       "│ 2   │ 9  │ 0.251662   │ 1.0 │\n",
       "│ 3   │ 7  │ 0.951916   │ 1.0 │\n",
       "│ 4   │ 10 │ 0.986666   │ 1.0 │\n",
       "│ 5   │ 8  │ 0.999905   │ 1.0 │\n",
       "│ 6   │ 4  │ 0.00790928 │ 0.0 │\n",
       "│ 7   │ 1  │ 0.236033   │ 0.0 │\n",
       "│ 8   │ 3  │ 0.312707   │ 0.0 │\n",
       "│ 9   │ 2  │ 0.346517   │ 0.0 │\n",
       "│ 10  │ 5  │ 0.488613   │ 0.0 │"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sort(x, (order(:y, rev=true), :x)) # the same as above"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>id</th><th>x</th><th>y</th></tr></thead><tbody><tr><th>1</th><td>8</td><td>0.999905</td><td>1.0</td></tr><tr><th>2</th><td>10</td><td>0.986666</td><td>1.0</td></tr><tr><th>3</th><td>7</td><td>0.951916</td><td>1.0</td></tr><tr><th>4</th><td>9</td><td>0.251662</td><td>1.0</td></tr><tr><th>5</th><td>6</td><td>0.210968</td><td>1.0</td></tr><tr><th>6</th><td>5</td><td>0.488613</td><td>0.0</td></tr><tr><th>7</th><td>2</td><td>0.346517</td><td>0.0</td></tr><tr><th>8</th><td>3</td><td>0.312707</td><td>0.0</td></tr><tr><th>9</th><td>1</td><td>0.236033</td><td>0.0</td></tr><tr><th>10</th><td>4</td><td>0.00790928</td><td>0.0</td></tr></tbody></table>"
      ],
      "text/plain": [
       "10×3 DataFrames.DataFrame\n",
       "│ Row │ id │ x          │ y   │\n",
       "├─────┼────┼────────────┼─────┤\n",
       "│ 1   │ 8  │ 0.999905   │ 1.0 │\n",
       "│ 2   │ 10 │ 0.986666   │ 1.0 │\n",
       "│ 3   │ 7  │ 0.951916   │ 1.0 │\n",
       "│ 4   │ 9  │ 0.251662   │ 1.0 │\n",
       "│ 5   │ 6  │ 0.210968   │ 1.0 │\n",
       "│ 6   │ 5  │ 0.488613   │ 0.0 │\n",
       "│ 7   │ 2  │ 0.346517   │ 0.0 │\n",
       "│ 8   │ 3  │ 0.312707   │ 0.0 │\n",
       "│ 9   │ 1  │ 0.236033   │ 0.0 │\n",
       "│ 10  │ 4  │ 0.00790928 │ 0.0 │"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sort(x, (order(:y, rev=true), order(:x, by=v->-v))) # some more fancy sorting stuff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>id</th><th>x</th><th>y</th></tr></thead><tbody><tr><th>1</th><td>8</td><td>0.999905</td><td>1.0</td></tr><tr><th>2</th><td>10</td><td>0.986666</td><td>1.0</td></tr><tr><th>3</th><td>7</td><td>0.951916</td><td>1.0</td></tr><tr><th>4</th><td>2</td><td>0.346517</td><td>0.0</td></tr><tr><th>5</th><td>5</td><td>0.488613</td><td>0.0</td></tr><tr><th>6</th><td>1</td><td>0.236033</td><td>0.0</td></tr><tr><th>7</th><td>6</td><td>0.210968</td><td>1.0</td></tr><tr><th>8</th><td>9</td><td>0.251662</td><td>1.0</td></tr><tr><th>9</th><td>4</td><td>0.00790928</td><td>0.0</td></tr><tr><th>10</th><td>3</td><td>0.312707</td><td>0.0</td></tr></tbody></table>"
      ],
      "text/plain": [
       "10×3 DataFrames.DataFrame\n",
       "│ Row │ id │ x          │ y   │\n",
       "├─────┼────┼────────────┼─────┤\n",
       "│ 1   │ 8  │ 0.999905   │ 1.0 │\n",
       "│ 2   │ 10 │ 0.986666   │ 1.0 │\n",
       "│ 3   │ 7  │ 0.951916   │ 1.0 │\n",
       "│ 4   │ 2  │ 0.346517   │ 0.0 │\n",
       "│ 5   │ 5  │ 0.488613   │ 0.0 │\n",
       "│ 6   │ 1  │ 0.236033   │ 0.0 │\n",
       "│ 7   │ 6  │ 0.210968   │ 1.0 │\n",
       "│ 8   │ 9  │ 0.251662   │ 1.0 │\n",
       "│ 9   │ 4  │ 0.00790928 │ 0.0 │\n",
       "│ 10  │ 3  │ 0.312707   │ 0.0 │"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[shuffle(1:10), :] # reorder rows (here randomly)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>id</th><th>x</th><th>y</th></tr></thead><tbody><tr><th>1</th><td>10</td><td>0.986666</td><td>1.0</td></tr><tr><th>2</th><td>2</td><td>0.346517</td><td>0.0</td></tr><tr><th>3</th><td>3</td><td>0.312707</td><td>0.0</td></tr><tr><th>4</th><td>4</td><td>0.00790928</td><td>0.0</td></tr><tr><th>5</th><td>5</td><td>0.488613</td><td>0.0</td></tr><tr><th>6</th><td>6</td><td>0.210968</td><td>1.0</td></tr><tr><th>7</th><td>7</td><td>0.951916</td><td>1.0</td></tr><tr><th>8</th><td>8</td><td>0.999905</td><td>1.0</td></tr><tr><th>9</th><td>9</td><td>0.251662</td><td>1.0</td></tr><tr><th>10</th><td>1</td><td>0.236033</td><td>0.0</td></tr></tbody></table>"
      ],
      "text/plain": [
       "10×3 DataFrames.DataFrame\n",
       "│ Row │ id │ x          │ y   │\n",
       "├─────┼────┼────────────┼─────┤\n",
       "│ 1   │ 10 │ 0.986666   │ 1.0 │\n",
       "│ 2   │ 2  │ 0.346517   │ 0.0 │\n",
       "│ 3   │ 3  │ 0.312707   │ 0.0 │\n",
       "│ 4   │ 4  │ 0.00790928 │ 0.0 │\n",
       "│ 5   │ 5  │ 0.488613   │ 0.0 │\n",
       "│ 6   │ 6  │ 0.210968   │ 1.0 │\n",
       "│ 7   │ 7  │ 0.951916   │ 1.0 │\n",
       "│ 8   │ 8  │ 0.999905   │ 1.0 │\n",
       "│ 9   │ 9  │ 0.251662   │ 1.0 │\n",
       "│ 10  │ 1  │ 0.236033   │ 0.0 │"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sort!(x, :id)\n",
    "x[[1,10],:] = x[[10,1],:] # swap rows\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>id</th><th>x</th><th>y</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>0.236033</td><td>0.0</td></tr><tr><th>2</th><td>2</td><td>0.346517</td><td>0.0</td></tr><tr><th>3</th><td>3</td><td>0.312707</td><td>0.0</td></tr><tr><th>4</th><td>4</td><td>0.00790928</td><td>0.0</td></tr><tr><th>5</th><td>5</td><td>0.488613</td><td>0.0</td></tr><tr><th>6</th><td>6</td><td>0.210968</td><td>1.0</td></tr><tr><th>7</th><td>7</td><td>0.951916</td><td>1.0</td></tr><tr><th>8</th><td>8</td><td>0.999905</td><td>1.0</td></tr><tr><th>9</th><td>9</td><td>0.251662</td><td>1.0</td></tr><tr><th>10</th><td>10</td><td>0.986666</td><td>1.0</td></tr></tbody></table>"
      ],
      "text/plain": [
       "10×3 DataFrames.DataFrame\n",
       "│ Row │ id │ x          │ y   │\n",
       "├─────┼────┼────────────┼─────┤\n",
       "│ 1   │ 1  │ 0.236033   │ 0.0 │\n",
       "│ 2   │ 2  │ 0.346517   │ 0.0 │\n",
       "│ 3   │ 3  │ 0.312707   │ 0.0 │\n",
       "│ 4   │ 4  │ 0.00790928 │ 0.0 │\n",
       "│ 5   │ 5  │ 0.488613   │ 0.0 │\n",
       "│ 6   │ 6  │ 0.210968   │ 1.0 │\n",
       "│ 7   │ 7  │ 0.951916   │ 1.0 │\n",
       "│ 8   │ 8  │ 0.999905   │ 1.0 │\n",
       "│ 9   │ 9  │ 0.251662   │ 1.0 │\n",
       "│ 10  │ 10 │ 0.986666   │ 1.0 │"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[1,:], x[10,:] = x[10,:], x[1,:] # and swap again\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Merging/adding rows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>x5</th></tr></thead><tbody><tr><th>1</th><td>0.0856352</td><td>0.185821</td><td>0.0516146</td><td>0.279395</td><td>0.370971</td></tr><tr><th>2</th><td>0.553206</td><td>0.111981</td><td>0.53803</td><td>0.178246</td><td>0.894166</td></tr><tr><th>3</th><td>0.46335</td><td>0.976312</td><td>0.455692</td><td>0.548983</td><td>0.648054</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×5 DataFrames.DataFrame\n",
       "│ Row │ x1        │ x2       │ x3        │ x4       │ x5       │\n",
       "├─────┼───────────┼──────────┼───────────┼──────────┼──────────┤\n",
       "│ 1   │ 0.0856352 │ 0.185821 │ 0.0516146 │ 0.279395 │ 0.370971 │\n",
       "│ 2   │ 0.553206  │ 0.111981 │ 0.53803   │ 0.178246 │ 0.894166 │\n",
       "│ 3   │ 0.46335   │ 0.976312 │ 0.455692  │ 0.548983 │ 0.648054 │"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = DataFrame(rand(3, 5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>x5</th></tr></thead><tbody><tr><th>1</th><td>0.0856352</td><td>0.185821</td><td>0.0516146</td><td>0.279395</td><td>0.370971</td></tr><tr><th>2</th><td>0.553206</td><td>0.111981</td><td>0.53803</td><td>0.178246</td><td>0.894166</td></tr><tr><th>3</th><td>0.46335</td><td>0.976312</td><td>0.455692</td><td>0.548983</td><td>0.648054</td></tr><tr><th>4</th><td>0.0856352</td><td>0.185821</td><td>0.0516146</td><td>0.279395</td><td>0.370971</td></tr><tr><th>5</th><td>0.553206</td><td>0.111981</td><td>0.53803</td><td>0.178246</td><td>0.894166</td></tr><tr><th>6</th><td>0.46335</td><td>0.976312</td><td>0.455692</td><td>0.548983</td><td>0.648054</td></tr></tbody></table>"
      ],
      "text/plain": [
       "6×5 DataFrames.DataFrame\n",
       "│ Row │ x1        │ x2       │ x3        │ x4       │ x5       │\n",
       "├─────┼───────────┼──────────┼───────────┼──────────┼──────────┤\n",
       "│ 1   │ 0.0856352 │ 0.185821 │ 0.0516146 │ 0.279395 │ 0.370971 │\n",
       "│ 2   │ 0.553206  │ 0.111981 │ 0.53803   │ 0.178246 │ 0.894166 │\n",
       "│ 3   │ 0.46335   │ 0.976312 │ 0.455692  │ 0.548983 │ 0.648054 │\n",
       "│ 4   │ 0.0856352 │ 0.185821 │ 0.0516146 │ 0.279395 │ 0.370971 │\n",
       "│ 5   │ 0.553206  │ 0.111981 │ 0.53803   │ 0.178246 │ 0.894166 │\n",
       "│ 6   │ 0.46335   │ 0.976312 │ 0.455692  │ 0.548983 │ 0.648054 │"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[x; x] # merge by rows - data frames must have the same column names; the same is vcat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x5</th><th>x4</th><th>x3</th><th>x2</th><th>x1</th></tr></thead><tbody><tr><th>1</th><td>0.370971</td><td>0.279395</td><td>0.0516146</td><td>0.185821</td><td>0.0856352</td></tr><tr><th>2</th><td>0.894166</td><td>0.178246</td><td>0.53803</td><td>0.111981</td><td>0.553206</td></tr><tr><th>3</th><td>0.648054</td><td>0.548983</td><td>0.455692</td><td>0.976312</td><td>0.46335</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×5 DataFrames.DataFrame\n",
       "│ Row │ x5       │ x4       │ x3        │ x2       │ x1        │\n",
       "├─────┼──────────┼──────────┼───────────┼──────────┼───────────┤\n",
       "│ 1   │ 0.370971 │ 0.279395 │ 0.0516146 │ 0.185821 │ 0.0856352 │\n",
       "│ 2   │ 0.894166 │ 0.178246 │ 0.53803   │ 0.111981 │ 0.553206  │\n",
       "│ 3   │ 0.648054 │ 0.548983 │ 0.455692  │ 0.976312 │ 0.46335   │"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = x[reverse(names(x))] # get y with other order of names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>x5</th></tr></thead><tbody><tr><th>1</th><td>0.0856352</td><td>0.185821</td><td>0.0516146</td><td>0.279395</td><td>0.370971</td></tr><tr><th>2</th><td>0.553206</td><td>0.111981</td><td>0.53803</td><td>0.178246</td><td>0.894166</td></tr><tr><th>3</th><td>0.46335</td><td>0.976312</td><td>0.455692</td><td>0.548983</td><td>0.648054</td></tr><tr><th>4</th><td>0.0856352</td><td>0.185821</td><td>0.0516146</td><td>0.279395</td><td>0.370971</td></tr><tr><th>5</th><td>0.553206</td><td>0.111981</td><td>0.53803</td><td>0.178246</td><td>0.894166</td></tr><tr><th>6</th><td>0.46335</td><td>0.976312</td><td>0.455692</td><td>0.548983</td><td>0.648054</td></tr></tbody></table>"
      ],
      "text/plain": [
       "6×5 DataFrames.DataFrame\n",
       "│ Row │ x1        │ x2       │ x3        │ x4       │ x5       │\n",
       "├─────┼───────────┼──────────┼───────────┼──────────┼──────────┤\n",
       "│ 1   │ 0.0856352 │ 0.185821 │ 0.0516146 │ 0.279395 │ 0.370971 │\n",
       "│ 2   │ 0.553206  │ 0.111981 │ 0.53803   │ 0.178246 │ 0.894166 │\n",
       "│ 3   │ 0.46335   │ 0.976312 │ 0.455692  │ 0.548983 │ 0.648054 │\n",
       "│ 4   │ 0.0856352 │ 0.185821 │ 0.0516146 │ 0.279395 │ 0.370971 │\n",
       "│ 5   │ 0.553206  │ 0.111981 │ 0.53803   │ 0.178246 │ 0.894166 │\n",
       "│ 6   │ 0.46335   │ 0.976312 │ 0.455692  │ 0.548983 │ 0.648054 │"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vcat(x, y) # we get what we want as vcat does column name matching"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "ename": "LoadError",
     "evalue": "\u001b[91mArgumentError: column(s) x1 and x2 are missing from argument(s) 2\u001b[39m",
     "output_type": "error",
     "traceback": [
      "\u001b[91mArgumentError: column(s) x1 and x2 are missing from argument(s) 2\u001b[39m",
      "",
      "Stacktrace:",
      " [1] \u001b[1m_vcat\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::Array{DataFrames.DataFrame,1}\u001b[1m)\u001b[22m\u001b[22m at \u001b[1mD:\\Software\\JULIA_PKG\\v0.6\\DataFrames\\src\\abstractdataframe\\abstractdataframe.jl:849\u001b[22m\u001b[22m",
      " [2] \u001b[1mvcat\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::DataFrames.DataFrame, ::DataFrames.DataFrame\u001b[1m)\u001b[22m\u001b[22m at \u001b[1mD:\\Software\\JULIA_PKG\\v0.6\\DataFrames\\src\\abstractdataframe\\abstractdataframe.jl:829\u001b[22m\u001b[22m"
     ]
    }
   ],
   "source": [
    "vcat(x, y[1:3]) # but column names must still match"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>x5</th></tr></thead><tbody><tr><th>1</th><td>0.0856352</td><td>0.185821</td><td>0.0516146</td><td>0.279395</td><td>0.370971</td></tr><tr><th>2</th><td>0.553206</td><td>0.111981</td><td>0.53803</td><td>0.178246</td><td>0.894166</td></tr><tr><th>3</th><td>0.46335</td><td>0.976312</td><td>0.455692</td><td>0.548983</td><td>0.648054</td></tr><tr><th>4</th><td>0.0856352</td><td>0.185821</td><td>0.0516146</td><td>0.279395</td><td>0.370971</td></tr><tr><th>5</th><td>0.553206</td><td>0.111981</td><td>0.53803</td><td>0.178246</td><td>0.894166</td></tr><tr><th>6</th><td>0.46335</td><td>0.976312</td><td>0.455692</td><td>0.548983</td><td>0.648054</td></tr></tbody></table>"
      ],
      "text/plain": [
       "6×5 DataFrames.DataFrame\n",
       "│ Row │ x1        │ x2       │ x3        │ x4       │ x5       │\n",
       "├─────┼───────────┼──────────┼───────────┼──────────┼──────────┤\n",
       "│ 1   │ 0.0856352 │ 0.185821 │ 0.0516146 │ 0.279395 │ 0.370971 │\n",
       "│ 2   │ 0.553206  │ 0.111981 │ 0.53803   │ 0.178246 │ 0.894166 │\n",
       "│ 3   │ 0.46335   │ 0.976312 │ 0.455692  │ 0.548983 │ 0.648054 │\n",
       "│ 4   │ 0.0856352 │ 0.185821 │ 0.0516146 │ 0.279395 │ 0.370971 │\n",
       "│ 5   │ 0.553206  │ 0.111981 │ 0.53803   │ 0.178246 │ 0.894166 │\n",
       "│ 6   │ 0.46335   │ 0.976312 │ 0.455692  │ 0.548983 │ 0.648054 │"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "append!(x, x) # the same but modifies x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "ename": "LoadError",
     "evalue": "\u001b[91mColumn names do not match\u001b[39m",
     "output_type": "error",
     "traceback": [
      "\u001b[91mColumn names do not match\u001b[39m",
      "",
      "Stacktrace:",
      " [1] \u001b[1mappend!\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::DataFrames.DataFrame, ::DataFrames.DataFrame\u001b[1m)\u001b[22m\u001b[22m at \u001b[1mD:\\Software\\JULIA_PKG\\v0.6\\DataFrames\\src\\dataframe\\dataframe.jl:970\u001b[22m\u001b[22m"
     ]
    }
   ],
   "source": [
    "append!(x, y) # here column names must match exactly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>x5</th></tr></thead><tbody><tr><th>1</th><td>0.0856352</td><td>0.185821</td><td>0.0516146</td><td>0.279395</td><td>0.370971</td></tr><tr><th>2</th><td>0.553206</td><td>0.111981</td><td>0.53803</td><td>0.178246</td><td>0.894166</td></tr><tr><th>3</th><td>0.46335</td><td>0.976312</td><td>0.455692</td><td>0.548983</td><td>0.648054</td></tr><tr><th>4</th><td>0.0856352</td><td>0.185821</td><td>0.0516146</td><td>0.279395</td><td>0.370971</td></tr><tr><th>5</th><td>0.553206</td><td>0.111981</td><td>0.53803</td><td>0.178246</td><td>0.894166</td></tr><tr><th>6</th><td>0.46335</td><td>0.976312</td><td>0.455692</td><td>0.548983</td><td>0.648054</td></tr><tr><th>7</th><td>1.0</td><td>2.0</td><td>3.0</td><td>4.0</td><td>5.0</td></tr></tbody></table>"
      ],
      "text/plain": [
       "7×5 DataFrames.DataFrame\n",
       "│ Row │ x1        │ x2       │ x3        │ x4       │ x5       │\n",
       "├─────┼───────────┼──────────┼───────────┼──────────┼──────────┤\n",
       "│ 1   │ 0.0856352 │ 0.185821 │ 0.0516146 │ 0.279395 │ 0.370971 │\n",
       "│ 2   │ 0.553206  │ 0.111981 │ 0.53803   │ 0.178246 │ 0.894166 │\n",
       "│ 3   │ 0.46335   │ 0.976312 │ 0.455692  │ 0.548983 │ 0.648054 │\n",
       "│ 4   │ 0.0856352 │ 0.185821 │ 0.0516146 │ 0.279395 │ 0.370971 │\n",
       "│ 5   │ 0.553206  │ 0.111981 │ 0.53803   │ 0.178246 │ 0.894166 │\n",
       "│ 6   │ 0.46335   │ 0.976312 │ 0.455692  │ 0.548983 │ 0.648054 │\n",
       "│ 7   │ 1.0       │ 2.0      │ 3.0       │ 4.0      │ 5.0      │"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "push!(x, 1:5) # add one row to x at the end; must give correct number of values and correct types\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th><th>x4</th><th>x5</th></tr></thead><tbody><tr><th>1</th><td>0.0856352</td><td>0.185821</td><td>0.0516146</td><td>0.279395</td><td>0.370971</td></tr><tr><th>2</th><td>0.553206</td><td>0.111981</td><td>0.53803</td><td>0.178246</td><td>0.894166</td></tr><tr><th>3</th><td>0.46335</td><td>0.976312</td><td>0.455692</td><td>0.548983</td><td>0.648054</td></tr><tr><th>4</th><td>0.0856352</td><td>0.185821</td><td>0.0516146</td><td>0.279395</td><td>0.370971</td></tr><tr><th>5</th><td>0.553206</td><td>0.111981</td><td>0.53803</td><td>0.178246</td><td>0.894166</td></tr><tr><th>6</th><td>0.46335</td><td>0.976312</td><td>0.455692</td><td>0.548983</td><td>0.648054</td></tr><tr><th>7</th><td>1.0</td><td>2.0</td><td>3.0</td><td>4.0</td><td>5.0</td></tr><tr><th>8</th><td>11.0</td><td>12.0</td><td>13.0</td><td>14.0</td><td>15.0</td></tr></tbody></table>"
      ],
      "text/plain": [
       "8×5 DataFrames.DataFrame\n",
       "│ Row │ x1        │ x2       │ x3        │ x4       │ x5       │\n",
       "├─────┼───────────┼──────────┼───────────┼──────────┼──────────┤\n",
       "│ 1   │ 0.0856352 │ 0.185821 │ 0.0516146 │ 0.279395 │ 0.370971 │\n",
       "│ 2   │ 0.553206  │ 0.111981 │ 0.53803   │ 0.178246 │ 0.894166 │\n",
       "│ 3   │ 0.46335   │ 0.976312 │ 0.455692  │ 0.548983 │ 0.648054 │\n",
       "│ 4   │ 0.0856352 │ 0.185821 │ 0.0516146 │ 0.279395 │ 0.370971 │\n",
       "│ 5   │ 0.553206  │ 0.111981 │ 0.53803   │ 0.178246 │ 0.894166 │\n",
       "│ 6   │ 0.46335   │ 0.976312 │ 0.455692  │ 0.548983 │ 0.648054 │\n",
       "│ 7   │ 1.0       │ 2.0      │ 3.0       │ 4.0      │ 5.0      │\n",
       "│ 8   │ 11.0      │ 12.0     │ 13.0      │ 14.0     │ 15.0     │"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "push!(x, Dict(:x1=> 11, :x2=> 12, :x3=> 13, :x4=> 14, :x5=> 15)) # also works with dictionaries\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Subsetting/removing rows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>id</th><th>val</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>'a'</td></tr><tr><th>2</th><td>2</td><td>'b'</td></tr><tr><th>3</th><td>3</td><td>'c'</td></tr><tr><th>4</th><td>4</td><td>'d'</td></tr><tr><th>5</th><td>5</td><td>'e'</td></tr><tr><th>6</th><td>6</td><td>'f'</td></tr><tr><th>7</th><td>7</td><td>'g'</td></tr><tr><th>8</th><td>8</td><td>'h'</td></tr><tr><th>9</th><td>9</td><td>'i'</td></tr><tr><th>10</th><td>10</td><td>'j'</td></tr></tbody></table>"
      ],
      "text/plain": [
       "10×2 DataFrames.DataFrame\n",
       "│ Row │ id │ val │\n",
       "├─────┼────┼─────┤\n",
       "│ 1   │ 1  │ 'a' │\n",
       "│ 2   │ 2  │ 'b' │\n",
       "│ 3   │ 3  │ 'c' │\n",
       "│ 4   │ 4  │ 'd' │\n",
       "│ 5   │ 5  │ 'e' │\n",
       "│ 6   │ 6  │ 'f' │\n",
       "│ 7   │ 7  │ 'g' │\n",
       "│ 8   │ 8  │ 'h' │\n",
       "│ 9   │ 9  │ 'i' │\n",
       "│ 10  │ 10 │ 'j' │"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = DataFrame(id=1:10, val='a':'j')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>id</th><th>val</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>'a'</td></tr><tr><th>2</th><td>2</td><td>'b'</td></tr></tbody></table>"
      ],
      "text/plain": [
       "2×2 DataFrames.DataFrame\n",
       "│ Row │ id │ val │\n",
       "├─────┼────┼─────┤\n",
       "│ 1   │ 1  │ 'a' │\n",
       "│ 2   │ 2  │ 'b' │"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[1:2, :] # by index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>id</th><th>val</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>'a'</td></tr><tr><th>2</th><td>2</td><td>'b'</td></tr></tbody></table>"
      ],
      "text/plain": [
       "2×2 DataFrames.SubDataFrame{UnitRange{Int64}}\n",
       "│ Row │ id │ val │\n",
       "├─────┼────┼─────┤\n",
       "│ 1   │ 1  │ 'a' │\n",
       "│ 2   │ 2  │ 'b' │"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "view(x, 1:2) # the same but a view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>id</th><th>val</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>'a'</td></tr><tr><th>2</th><td>3</td><td>'c'</td></tr><tr><th>3</th><td>5</td><td>'e'</td></tr><tr><th>4</th><td>7</td><td>'g'</td></tr><tr><th>5</th><td>9</td><td>'i'</td></tr></tbody></table>"
      ],
      "text/plain": [
       "5×2 DataFrames.DataFrame\n",
       "│ Row │ id │ val │\n",
       "├─────┼────┼─────┤\n",
       "│ 1   │ 1  │ 'a' │\n",
       "│ 2   │ 3  │ 'c' │\n",
       "│ 3   │ 5  │ 'e' │\n",
       "│ 4   │ 7  │ 'g' │\n",
       "│ 5   │ 9  │ 'i' │"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[repmat([true, false], 5), :] # by Bool, exact length required"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>id</th><th>val</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>'a'</td></tr><tr><th>2</th><td>3</td><td>'c'</td></tr><tr><th>3</th><td>5</td><td>'e'</td></tr><tr><th>4</th><td>7</td><td>'g'</td></tr><tr><th>5</th><td>9</td><td>'i'</td></tr></tbody></table>"
      ],
      "text/plain": [
       "5×2 DataFrames.SubDataFrame{Array{Int64,1}}\n",
       "│ Row │ id │ val │\n",
       "├─────┼────┼─────┤\n",
       "│ 1   │ 1  │ 'a' │\n",
       "│ 2   │ 3  │ 'c' │\n",
       "│ 3   │ 5  │ 'e' │\n",
       "│ 4   │ 7  │ 'g' │\n",
       "│ 5   │ 9  │ 'i' │"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "view(x, repmat([true, false], 5), :) # view again"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>id</th><th>val</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>'a'</td></tr><tr><th>2</th><td>2</td><td>'b'</td></tr><tr><th>3</th><td>3</td><td>'c'</td></tr><tr><th>4</th><td>4</td><td>'d'</td></tr><tr><th>5</th><td>5</td><td>'e'</td></tr><tr><th>6</th><td>6</td><td>'f'</td></tr><tr><th>7</th><td>8</td><td>'h'</td></tr><tr><th>8</th><td>9</td><td>'i'</td></tr><tr><th>9</th><td>10</td><td>'j'</td></tr></tbody></table>"
      ],
      "text/plain": [
       "9×2 DataFrames.DataFrame\n",
       "│ Row │ id │ val │\n",
       "├─────┼────┼─────┤\n",
       "│ 1   │ 1  │ 'a' │\n",
       "│ 2   │ 2  │ 'b' │\n",
       "│ 3   │ 3  │ 'c' │\n",
       "│ 4   │ 4  │ 'd' │\n",
       "│ 5   │ 5  │ 'e' │\n",
       "│ 6   │ 6  │ 'f' │\n",
       "│ 7   │ 8  │ 'h' │\n",
       "│ 8   │ 9  │ 'i' │\n",
       "│ 9   │ 10 │ 'j' │"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "deleterows!(x, 7) # delete one row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>id</th><th>val</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>'a'</td></tr><tr><th>2</th><td>2</td><td>'b'</td></tr><tr><th>3</th><td>3</td><td>'c'</td></tr><tr><th>4</th><td>4</td><td>'d'</td></tr><tr><th>5</th><td>5</td><td>'e'</td></tr><tr><th>6</th><td>9</td><td>'i'</td></tr><tr><th>7</th><td>10</td><td>'j'</td></tr></tbody></table>"
      ],
      "text/plain": [
       "7×2 DataFrames.DataFrame\n",
       "│ Row │ id │ val │\n",
       "├─────┼────┼─────┤\n",
       "│ 1   │ 1  │ 'a' │\n",
       "│ 2   │ 2  │ 'b' │\n",
       "│ 3   │ 3  │ 'c' │\n",
       "│ 4   │ 4  │ 'd' │\n",
       "│ 5   │ 5  │ 'e' │\n",
       "│ 6   │ 9  │ 'i' │\n",
       "│ 7   │ 10 │ 'j' │"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "deleterows!(x, 6:7) # delete a collection of rows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>2</td><td>3</td></tr><tr><th>2</th><td>2</td><td>3</td><td>4</td></tr><tr><th>3</th><td>3</td><td>4</td><td>5</td></tr><tr><th>4</th><td>4</td><td>5</td><td>6</td></tr></tbody></table>"
      ],
      "text/plain": [
       "4×3 DataFrames.DataFrame\n",
       "│ Row │ x1 │ x2 │ x3 │\n",
       "├─────┼────┼────┼────┤\n",
       "│ 1   │ 1  │ 2  │ 3  │\n",
       "│ 2   │ 2  │ 3  │ 4  │\n",
       "│ 3   │ 3  │ 4  │ 5  │\n",
       "│ 4   │ 4  │ 5  │ 6  │"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = DataFrame([1:4, 2:5, 3:6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th></tr></thead><tbody><tr><th>1</th><td>3</td><td>4</td><td>5</td></tr><tr><th>2</th><td>4</td><td>5</td><td>6</td></tr></tbody></table>"
      ],
      "text/plain": [
       "2×3 DataFrames.DataFrame\n",
       "│ Row │ x1 │ x2 │ x3 │\n",
       "├─────┼────┼────┼────┤\n",
       "│ 1   │ 3  │ 4  │ 5  │\n",
       "│ 2   │ 4  │ 5  │ 6  │"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "filter(r -> r[:x1] > 2.5, x) # create a new DataFrame where filtering function operates on DataFrameRow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x1</th><th>x2</th><th>x3</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>2</td><td>3</td></tr><tr><th>2</th><td>3</td><td>4</td><td>5</td></tr></tbody></table>"
      ],
      "text/plain": [
       "2×3 DataFrames.DataFrame\n",
       "│ Row │ x1 │ x2 │ x3 │\n",
       "├─────┼────┼────┼────┤\n",
       "│ 1   │ 1  │ 2  │ 3  │\n",
       "│ 2   │ 3  │ 4  │ 5  │"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# in place modification of x, an example with do-block syntax\n",
    "filter!(x) do r\n",
    "    if r[:x1] > 2.5\n",
    "        return r[:x2] < 4.5\n",
    "    end\n",
    "    r[:x3] < 3.5\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Deduplicating"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>A</th><th>B</th><th>C</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>x</td><td>1</td></tr><tr><th>2</th><td>2</td><td>y</td><td>2</td></tr><tr><th>3</th><td>1</td><td>x</td><td>3</td></tr><tr><th>4</th><td>2</td><td>y</td><td>4</td></tr></tbody></table>"
      ],
      "text/plain": [
       "4×3 DataFrames.DataFrame\n",
       "│ Row │ A │ B │ C │\n",
       "├─────┼───┼───┼───┤\n",
       "│ 1   │ 1 │ x │ 1 │\n",
       "│ 2   │ 2 │ y │ 2 │\n",
       "│ 3   │ 1 │ x │ 3 │\n",
       "│ 4   │ 2 │ y │ 4 │"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = DataFrame(A=[1,2], B=[\"x\",\"y\"])\n",
    "append!(x, x)\n",
    "x[:C] = 1:4\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>A</th><th>B</th><th>C</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>x</td><td>1</td></tr><tr><th>2</th><td>2</td><td>y</td><td>2</td></tr></tbody></table>"
      ],
      "text/plain": [
       "2×3 DataFrames.DataFrame\n",
       "│ Row │ A │ B │ C │\n",
       "├─────┼───┼───┼───┤\n",
       "│ 1   │ 1 │ x │ 1 │\n",
       "│ 2   │ 2 │ y │ 2 │"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "unique(x, [1,2]) # get first unique rows for given index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>A</th><th>B</th><th>C</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>x</td><td>1</td></tr><tr><th>2</th><td>2</td><td>y</td><td>2</td></tr><tr><th>3</th><td>1</td><td>x</td><td>3</td></tr><tr><th>4</th><td>2</td><td>y</td><td>4</td></tr></tbody></table>"
      ],
      "text/plain": [
       "4×3 DataFrames.DataFrame\n",
       "│ Row │ A │ B │ C │\n",
       "├─────┼───┼───┼───┤\n",
       "│ 1   │ 1 │ x │ 1 │\n",
       "│ 2   │ 2 │ y │ 2 │\n",
       "│ 3   │ 1 │ x │ 3 │\n",
       "│ 4   │ 2 │ y │ 4 │"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "unique(x) # now we look at whole rows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4-element Array{Bool,1}:\n",
       " false\n",
       " false\n",
       "  true\n",
       "  true"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nonunique(x, :A) # get indicators of non-unique rows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>A</th><th>B</th><th>C</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>x</td><td>1</td></tr><tr><th>2</th><td>2</td><td>y</td><td>2</td></tr></tbody></table>"
      ],
      "text/plain": [
       "2×3 DataFrames.DataFrame\n",
       "│ Row │ A │ B │ C │\n",
       "├─────┼───┼───┼───┤\n",
       "│ 1   │ 1 │ x │ 1 │\n",
       "│ 2   │ 2 │ y │ 2 │"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "unique!(x, :B) # modify x in place"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Extracting one row from `DataFrame` into a vector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"data-frame\"><thead><tr><th></th><th>x</th><th>y</th><th>z</th></tr></thead><tbody><tr><th>1</th><td>1</td><td>a</td><td>true</td></tr><tr><th>2</th><td>missing</td><td>b</td><td>false</td></tr><tr><th>3</th><td>2</td><td>missing</td><td>true</td></tr></tbody></table>"
      ],
      "text/plain": [
       "3×3 DataFrames.DataFrame\n",
       "│ Row │ x       │ y       │ z     │\n",
       "├─────┼─────────┼─────────┼───────┤\n",
       "│ 1   │ 1       │ a       │ true  │\n",
       "│ 2   │ \u001b[90mmissing\u001b[39m │ b       │ false │\n",
       "│ 3   │ 2       │ \u001b[90mmissing\u001b[39m │ true  │"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = DataFrame(x=[1,missing,2], y=[\"a\", \"b\", missing], z=[true,false,true])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2-element Array{Any,1}:\n",
       " 1   \n",
       "  \"a\""
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cols = [:x, :y]\n",
    "[x[1, col] for col in cols] # subset of columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3-element Array{Array{Any,1},1}:\n",
       " Any[1, \"a\", true]       \n",
       " Any[missing, \"b\", false]\n",
       " Any[2, missing, true]   "
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[[x[i, col] for col in names(x)] for i in 1:nrow(x)] # vector of vectors, each entry contains one full row of x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, \"a\")"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Tuple(x[1, col] for col in cols) # similar construct for Tuples, when ported to Julia 0.7 NamedTuples will be added"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.6.2",
   "language": "julia",
   "name": "julia-0.6"
  },
  "language_info": {
   "file_extension": ".jl",
   "mimetype": "application/julia",
   "name": "julia",
   "version": "0.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
